import {queryOrderList} from "@src/api/booking";
import dayjs from "dayjs";
import {deleteOrder} from "@src/api/order";
import router from "@src/router";

Component({
  behaviors: [router.routerBehavior],
  options: {
    addGlobalClass: true
  },
  data: {
    loading: false,
    hasNextPage: false,
    pageNum: 1,
    pageSize: 10,
    startX: 0,
    startY: 0,
    dayList: []
  },
  lifetimes: {
    ready: function () {
      this.fetchData();
    }
  },
  methods: {
    fetchData() {
      let data = {orderType: 7, pageNum: this.data.pageNum, pageSize: this.data.pageSize}
      this.setData({loading: true})
      queryOrderList(data).then(res => {
        wx.stopPullDownRefresh();
        this.setData({loading: false})
        if (res.code === 0) {
          this.parseList(res.data.list);
          this.setData({hasNextPage: res.data.hasNextPage})
        }
      }).catch(() => {
        this.setData({loading: false})
      })
    },
    parseList(newList) {
      if (!newList || newList.length === 0) {
        return;
      }
      let dayMap = {}
      let lastDay = null;
      let dayList = this.data.dayList;
      if (dayList.length > 0) {
        lastDay = dayList[dayList.length - 1].day;
      }
      for (let order of newList) {
        let day = dayjs(order.createTime).format("YYYY-MM-DD");
        if (dayMap[day] === undefined) {
          dayMap[day] = [];
        }
        order.isTouchMove = false;
        order.bizData = JSON.parse(order.bizData);
        order.bizData.days = Object.keys(order.bizData.roomPrices).length
        dayMap[day].push(order);
      }
      for (let day in dayMap) {
        if (lastDay !== null && day === lastDay) {
          dayList[dayList.length - 1].list.push(...dayMap[day])
        } else {
          dayList.push({day: day, list: dayMap[day]})
        }
      }
      this.setData({dayList: dayList})
    },
    handleDel(e) {
      let order = e.currentTarget.dataset.order;
      let index = e.currentTarget.dataset.index;
      let subIndex = e.currentTarget.dataset.subindex;

      if (order.subStatus === 0 || order.subStatus === 1 || order.subStatus === 2) {
        wx.showModal({
          title: "未完成的订单",
          content: "订单当前状态不可删除，请完成或取消订单后删除。",
          confirmText: "好的",
          showCancel: false,
          success(res) {
            if (res.confirm) {
            } else if (res.cancel) {
            }
          }
        });
        return;
      }
      wx.showModal({
        title: "确认删除订单？",
        content: "删除后订单将无法恢复，确认要删除吗？",
        cancelText: "点错了",
        confirmText: "删除",
        confirmColor: "#F56C6C",
        success: (res) => {
          if (res.confirm) {
            deleteOrder({orderNo: order.orderNo}).then(res => {
              this.data.dayList[index].list.splice(subIndex, 1);
              if (this.data.dayList[index].list.length === 0) {
                this.data.dayList.splice(index, 1);
              }
              this.setData({dayList: this.data.dayList})
              wx.showToast({title: "已删除", icon: "success", duration: 2000});
            });
          } else if (res.cancel) {
          }
        }
      });
    },
    /**
     * 左划动删除
     */
    bookTouchstart(e) {
      if (e.currentTarget.dataset.mode === "del") return;
      //开始触摸时 重置所有删除
      let dayList = this.data.dayList;
      dayList.forEach(v => {
        v.list.forEach(subV => {
          if (subV.isTouchMove) {
            subV.isTouchMove = false;
          }
        });
      });
      console.info(e)
      this.setData({dayList: dayList, startX: e.touches[0].clientX, startY: e.touches[0].clientY})
    },
    bookTouchmove(e) {
      let index = e.currentTarget.dataset.index, //当前索引
          subIndex = e.currentTarget.dataset.subindex, //当前索引
          startX = this.data.startX, //开始X坐标
          startY = this.data.startY, //开始Y坐标
          touchMoveX = e.touches[0].clientX, //滑动变化坐标
          touchMoveY = e.touches[0].clientY, //滑动变化坐标
          //获取滑动角度
          angle = this.angle(
              {X: startX, Y: startY},
              {X: touchMoveX, Y: touchMoveY}
          );
      if (Math.abs(angle) > 20 || Math.abs(touchMoveX - startX) < 30) return;
      if (touchMoveX > startX) {
        this.data.dayList[index].list[subIndex].isTouchMove = false;
      } else {
        this.data.dayList[index].list[subIndex].isTouchMove = true;
      }
      this.setData({dayList: this.data.dayList})
    },
    angle: function (start, end) {
      var _X = end.X - start.X,
          _Y = end.Y - start.Y;
      return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
    },
    onReachBottom() {
      if (this.data.hasNextPage) {
        this.setData({pageNum: this.data.pageNum + 1});
        this.fetchData();
      }
    },
    onPullDownRefresh() {
      wx.stopPullDownRefresh();
      this.setData({pageNum: 1, pageSize: 10, hasNextPage: false, dayList: []});
      this.fetchData();
    }
  }
});
